<%@ page import="java.lang.String, ThematicModelProto.*, java.util.*, java.text.*, java.util.Iterator, java.util.ArrayList, com.aetrion.flickr.*" %>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="style.css" />
	</head>
	<body>
		<%
		ArrayList selectedThemes = new ArrayList();
		String req = request.getParameter("themes");
		String[] reqarray = req.split(", ");
		for(int x = 0; x<reqarray.length; x++){
			selectedThemes.add(reqarray[x]);
		}
		
		
		ThematicModelBuilder tmb = new ThematicModelBuilder("D:\\PhD\\Thematics\\implementation\\"+request.getParameter("model")+"\\");
		ArrayList<Theme> themeList = new ArrayList();
		//themeList.add(tmb.themes.get("winter"));
		//themeList.add(tmb.themes.get("family"));
		out.println("<b>"+request.getParameter("keyword")+"</b> <br/> With the themes of: <br/>");
		for(int i = 0; i<selectedThemes.size(); i++){
			themeList.add(tmb.themes.get(selectedThemes.get(i)));
			System.out.println("Selected Theme: "+selectedThemes.get(i));
			out.println(selectedThemes.get(i)+"<br/>");
		}
		
		ArrayList<Natom> nList = new ArrayList();
		
		if(request.getParameter("keyword").equals("")){
			//nList = tmb.getStorySelection(themeList, 10);
		}
		else{
			nList = tmb.getStorySelection(request.getParameter("keyword"), new Integer(request.getParameter("fsize")).intValue(),themeList, 10,false);
		}
		
		for(int i = 0; i<nList.size(); i++){
			String tags= "";
			for(int t = 0; t<nList.get(i).features.size(); t++){
				tags+=nList.get(i).features.get(t);
				if(t!=nList.get(i).features.size()-1)
					tags+=", ";
			}
			String scoretext = "Motif Coverage: "+nList.get(i).motifScore+"% <br/> <table><tr><td>Motif</td><td>Focus</td><td>Match</td></tr>";
			Iterator<String> it = nList.get(i).motifsmatch.keySet().iterator();
			while(it.hasNext()){
				String mn = it.next();
				scoretext+="<tr><td>"+mn+"</td><td>"+nList.get(i).motifsmatch.get(mn)[0]+"%</td><td> "+nList.get(i).motifsmatch.get(mn)[1]+"%</td></tr>";
			}
			scoretext+="</table>";
			scoretext+="Component Coverage: "+nList.get(i).componentScore+"% <br/> <table><tr><td>Component</td><td>Focus</td><td>Match</td></tr>";
			for(int x = 0; x<themeList.size(); x++){
				if(themeList.get(x).required.size()>0){
					for(int y = 0; y<themeList.get(x).required.size(); y++){
						if(tmb.themes.containsKey(themeList.get(x).required.get(y)))
							scoretext+="<tr><td>"+themeList.get(x).required.get(y)+"</td><td>"+tmb.calculateComponentFocus(tmb.themes.get(themeList.get(x).required.get(y).toLowerCase()), nList.get(i))+"%</td><td> "+tmb.calculateComponentMatch(tmb.themes.get(themeList.get(x).required.get(y).toLowerCase()), nList.get(i))+"%</td></tr>";
						else
							scoretext+="<tr><td>"+themeList.get(x).required.get(y)+"</td><td>"+tmb.calculateComponentFocus(tmb.motifs.get(themeList.get(x).required.get(y).toLowerCase()), nList.get(i))+"%</td><td> "+tmb.calculateComponentMatch(tmb.motifs.get(themeList.get(x).required.get(y).toLowerCase()), nList.get(i))+"%</td></tr>";
					
					}
				}
				if(themeList.get(x).optional.size()>0){
					for(int z = 0; z<themeList.get(x).optional.size(); z++){
						if(tmb.themes.containsKey(themeList.get(x).optional.get(z)))
							scoretext+="<tr><td>"+themeList.get(x).optional.get(z)+"</td><td>"+tmb.calculateComponentFocus(tmb.themes.get(themeList.get(x).optional.get(z).toLowerCase()), nList.get(i))+"%</td><td> "+tmb.calculateComponentMatch(tmb.themes.get(themeList.get(x).optional.get(z).toLowerCase()), nList.get(i))+"%</td></tr>";
						else
							scoretext+="<tr><td>"+themeList.get(x).optional.get(z)+"</td><td>"+tmb.calculateComponentFocus(tmb.motifs.get(themeList.get(x).optional.get(z).toLowerCase()), nList.get(i))+"%</td><td> "+tmb.calculateComponentMatch(tmb.motifs.get(themeList.get(x).optional.get(z).toLowerCase()), nList.get(i))+"%</td></tr>";
					
					}
				}
			
			}
			scoretext+="</table>";
			scoretext+="Theme Coverage: "+nList.get(i).thematicScore+"% <br/> <table><tr><td>Theme</td><td>Focus</td><td>Match</td></tr>";;
			for(int k = 0; k<themeList.size(); k++){
				scoretext+="<tr><td>"+themeList.get(k).name+"</td><td>"+tmb.calculateThematicFocus(themeList.get(k), nList.get(i))+"%</td><td> "+tmb.calculateThematicMatch(themeList.get(k), nList.get(i))+"%</td></tr>";
			}
			scoretext+="</table>";
			out.print("<div>");		
			out.print("<img align='top' src='"+nList.get(i).url+"'  alt='"+tags+"' style+'float:left' />");
			out.print("<div>"+scoretext+"</div>");
			out.print("</div>");
			out.print("<hr />");		
		}
		
		out.println("<br/>");
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new java.util.Date();
		out.println("Current Date Time : " + dateFormat.format(date));

		
		
		%>
	</body>
</html>